//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Tibor.h"
using namespace Cephei::QL::Indexes::Ibor;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Currency.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Indexes::Ibor::CTibor::CTibor (Cephei::QL::Times::IPeriod^ tenor, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ h) : CIborIndex(CTibor::typeid)
{
    CPeriod^ _Ctenor;
    CYieldTermStructure^ _Ch;
    try
    {
#ifdef HANDLE
        _phTibor = NULL;
#endif
        _Ctenor = safe_cast<CPeriod^> (tenor);
        _Ctenor->Lock();
        QuantLib::Period& _tenor = static_cast<QuantLib::Period&> (_Ctenor->GetReference ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (h))
        {
            _Ch = safe_cast<CYieldTermStructure^> (h->Value);
            _Ch->Lock();
        }
        Handle<QuantLib::YieldTermStructure>& _h = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (h) ? static_cast<Handle<QuantLib::YieldTermStructure>&> (_Ch->GetHandle ()) : Handle<QuantLib::YieldTermStructure>()); //1
        _ppTibor = new boost::shared_ptr<QuantLib::Tibor> (new QuantLib::Tibor ( _tenor,  _h ));
        SetIborIndex (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppTibor));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ctenor != nullptr) _Ctenor->Unlock();
        if (_Ch != nullptr) _Ch->Unlock();
    }
}
Cephei::QL::Indexes::Ibor::CTibor::CTibor (boost::shared_ptr<QuantLib::Tibor>& childNative, Object^ owner) : CIborIndex(CTibor::typeid)
{
#ifdef HANDLE
	_phTibor = NULL;
#endif
	_ppTibor = &childNative;
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppTibor));
}
Cephei::QL::Indexes::Ibor::CTibor::CTibor (QuantLib::Tibor& childNative, Object^ owner) : CIborIndex(CTibor::typeid)
{
#ifdef HANDLE
	_phTibor = NULL;
#endif
	_ppTibor = new boost::shared_ptr<QuantLib::Tibor> (&childNative);
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppTibor));
    _TiborOwner = owner;
    _IborIndexOwner = owner;
}

Cephei::QL::Indexes::Ibor::CTibor::CTibor (CTibor^ copy) : CIborIndex(CTibor::typeid)
{
#ifdef HANDLE
	_phTibor = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppTibor = new boost::shared_ptr<QuantLib::Tibor> (copy->GetShared());
        _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppTibor));
    }
}
Cephei::QL::Indexes::Ibor::CTibor::CTibor (PLATFORM::Type^ t) : CIborIndex(CTibor::typeid)
{
#ifdef HANDLE
	_phTibor = NULL;
#endif
	if (!t->IsSubclassOf(CTibor::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Indexes::Ibor::CTibor::CTibor (QuantLib::Handle<QuantLib::Tibor>& childNative, Object^ owner)  : CIborIndex(CTibor::typeid)
{
	_phTibor = &childNative;
	_ppTibor = &static_cast<boost::shared_ptr<QuantLib::Tibor>>(childNative.currentLink());
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppTibor));
    _TiborOwner = owner;
}
Cephei::QL::Indexes::Ibor::CTibor::CTibor (QuantLib::Handle<QuantLib::Tibor> childNative)  : CIborIndex(CTibor::typeid)
{
	_phTibor = &childNative;
	_ppTibor = &static_cast<boost::shared_ptr<QuantLib::Tibor>>(childNative.currentLink());
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppTibor));
}
#endif
#ifdef STRUCT
Cephei::QL::Indexes::Ibor::CTibor::CTibor (QuantLib::Tibor childNative)  : CIborIndex(CTibor::typeid)
{
#ifdef HANDLE
	_phTibor = NULL;
#endif
	_ppTibor = new boost::shared_ptr<QuantLib::Tibor> (new QuantLib::Tibor (childNative));
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppTibor));
}
#endif

Cephei::QL::Indexes::Ibor::CTibor::~CTibor ()
{
    if (_ppTibor != NULL)
    {
	    delete _ppTibor;
        _ppTibor = NULL;
    }
}
Cephei::QL::Indexes::Ibor::CTibor::!CTibor ()
{
    if (_ppTibor != NULL)
    {
	    delete _ppTibor;
    }
}
QuantLib::Tibor& Cephei::QL::Indexes::Ibor::CTibor::GetReference ()
{
    if (_ppTibor == NULL) throw REFNEW NativeNullException ();
	return **_ppTibor;
}
boost::shared_ptr<QuantLib::Tibor>& Cephei::QL::Indexes::Ibor::CTibor::GetShared ()
{
    if (_ppTibor == NULL) throw REFNEW NativeNullException ();
	return *_ppTibor;
}
QuantLib::Tibor* Cephei::QL::Indexes::Ibor::CTibor::GetPointer ()
{
    if (_ppTibor == NULL) throw REFNEW NativeNullException ();
	return &**_ppTibor;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Tibor>& Cephei::QL::Indexes::Ibor::CTibor::GetHandle ()
{
	if (_phTibor == NULL)
	{
		_phTibor = new Handle<QuantLib::Tibor> (*_ppTibor);
	}
	return *_phTibor;
}
#endif
bool Cephei::QL::Indexes::Ibor::CTibor::HasNative () 
{
	return (_ppTibor != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Indexes::Ibor::ITibor^ Cephei::QL::Indexes::Ibor::CTibor_Factory::Create (Cephei::QL::Times::IPeriod^ tenor, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ h)
{
    return REFNEW CTibor ( tenor,  h);
}
